package com.wxq.demo.controller;

import com.wxq.demo.dao.BookDao;
import com.wxq.demo.pojo.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
public class BookController {
    @Autowired
    private BookDao bookDao;
    @Autowired
    private MongoTemplate mongoTemplate;

    @GetMapping("/test")
    public void test() {
        List<Book> books = new ArrayList<>();
        Book book1 = new Book();
        book1.setId(1);
        book1.setName("朝花夕拾");
        book1.setAuthor("鲁迅");
        books.add(book1);

        Book book2 = new Book();
        book2.setId(2);
        book2.setName("呐喊");
        book2.setAuthor("鲁迅");
        books.add(book2);
        bookDao.insert(books);

        List<Book> bookList = bookDao.findByAuthorContains("鲁迅");
        for (Book book : bookList) {
            System.out.println(book.getId() + ";" + book.getName() + ";" + book.getAuthor());
        }

        List<Book> books1 = mongoTemplate.findAll(Book.class);
        for (Book book : books1) {
            System.out.println(book.getId() + ";" + book.getName() + ";" + book.getAuthor());
        }

        Book book = bookDao.findByNameEquals("朝花夕拾");
        System.out.println(book.getId() + ";" + book.getName() + ";" + book.getAuthor());
    }
}
